import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import basis from './views/VUEBasis.vue'
import Roast from './views/Roast.vue'
import MyResume from './views/MyResume.vue'
import VIP from './views/vipPage.vue'

// 二级路由
import author from './views/AboutCompontents/contact.vue'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [{
      path: '/',
      name: 'homeLink',
      component: Home
    },
    {
      path: '/roast',
      name: 'roastLink',
      component: Roast
    },
    {
      path: '/basis',
      name: 'basisLink',
      component: basis
    },
    {
      path: '/myresume',
      name: 'MyResumeLink',
      component: MyResume
    },
    {
      path: '/vip',
      name: 'VIP',
      component: VIP
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import( /* webpackChunkName: "about" */ './views/About.vue'),
      children: [{
        path: '/author',
        name: 'authorLink',
        // redirect: '/author',
        component: author
      }]
    },
    {
      // 用于访问不存在的路径的时候 跳转到首页
      path: '*',
      redirect: '/'
    },
  ]
})

// 全局守卫
router.beforeEach((to, from, next) => {
  // console.log(to)
  // console.log(from)
  // console.log(next)

  // 进行判断
  // if(to.path != '/about'){
  next()
  // } else {
  //   alert('必须经过作者同意，才可以看到关于页面')
  // }
})


router.beforeEach((to, from, next) => {
  if (to.matched.length === 0) { //匹配前往的路由不存在
    from.name ? next({
      name: from.name
    }) : next('/'); //判断此跳转路由的来源路由是否存在，存在的情况跳转到来源路由，否则跳转到404页面
  } else {
    next(); //如果匹配到正确跳转
  }
});


export default router